package com.gwnet.common.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceHttpClientImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedissonConfigImpl;
import com.gwnet.common.bean.WxMiniApp;
import lombok.AllArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

/**
 * 通过微信配置信息，获取微信相关配置
 * @author: WangLiang
 * @date: 2024/7/20 9:51
 */
@Component
@AllArgsConstructor
public class WxConfig {

    private final RedissonClient redissonClient;

    private final PlatformConfig platformConfig;

    private static final String MA_CONFIG_KEY_PREFIX = "gwnet:ma_config_key_prefix";

    private static final String MA_RIDER_CONFIG_KEY_PREFIX = "gwnet:ma_rider_config_key_prefix";

    /**
     * 获取微信用户端小程序服务
     * @return
     */
    public WxMaService getWxMaService() {
        WxMiniApp wxMiniApp = platformConfig.getWxMiniApp();
        // access token用redis缓存
        WxMaRedissonConfigImpl maRedissonConfig = new WxMaRedissonConfigImpl(redissonClient, MA_CONFIG_KEY_PREFIX);
        maRedissonConfig.setAppid(wxMiniApp.getAppId());
        maRedissonConfig.setSecret(wxMiniApp.getSecret());

        WxMaServiceHttpClientImpl wxMaServiceHttpClient = new WxMaServiceHttpClientImpl();
        wxMaServiceHttpClient.setWxMaConfig(maRedissonConfig);
        return wxMaServiceHttpClient;
    }

    /**
     * 获取骑手端小程序服务
     * @return
     */
    public WxMaService getWxMaRiderService() {
        WxMiniApp wxMiniApp = platformConfig.getWxRiderMiniApp();
        // access token用redis缓存
        WxMaRedissonConfigImpl maRedissonConfig = new WxMaRedissonConfigImpl(redissonClient, MA_RIDER_CONFIG_KEY_PREFIX);
        maRedissonConfig.setAppid(wxMiniApp.getAppId());
        maRedissonConfig.setSecret(wxMiniApp.getSecret());

        WxMaServiceHttpClientImpl wxMaServiceHttpClient = new WxMaServiceHttpClientImpl();
        wxMaServiceHttpClient.setWxMaConfig(maRedissonConfig);
        return wxMaServiceHttpClient;
    }


}
